// 导入通用任务注册工具
import { registerHandler } from '$lib/utils/workers/base-worker';
import { type TableData } from './table';

// 生成大量测试数据的函数（原封不动迁移到 Worker 中）
function generateTestData(count: number) : TableData[] {
    const surnames = ['张', '王', '李', '赵', '刘', '陈', '杨', '黄', '周', '吴'];
    const firstNamesMale = ['伟', '强', '勇', '军', '杰', '涛', '磊', '超', '明', '辉'];
    const firstNamesFemale = ['芳', '娜', '秀英', '敏', '静', '丽', '强', '磊', '军', '洋'];
    const districts = ['浦东新区', '徐汇区', '长宁区', '静安区', '普陀区', '虹口区', '杨浦区', '黄浦区'];
    const streets = ['金沙江路', '南京东路', '淮海中路', '世纪大道', '延安西路', '中山路', '长寿路', '虹桥路'];
    const positions = ['经理', '主管', '专员', '助理', '工程师', '设计师', '分析师', '顾问'];
    const departments = ['研发部', '市场部', '人力资源部', '财务部', '运营部', '产品部', '销售部', '客服部'];

    function generateRandomDate() {
        const now = new Date();
        const past = new Date(now.getFullYear() - 5, now.getMonth(), now.getDate());
        const randomTime = past.getTime() + Math.random() * (now.getTime() - past.getTime());
        const randomDate = new Date(randomTime);
        return randomDate.toISOString().split('T')[0];
    }

    function generateRandomPhone() {
        const prefixes = ['130', '131', '132', '133', '134', '135', '136', '137', '138', '139', '150', '151', '152', '153', '155', '156', '157', '158', '159', '170', '171', '173', '175', '176', '177', '178', '180', '181', '182', '183', '184', '185', '186', '187', '188', '189'];
        const prefix = prefixes[Math.floor(Math.random() * prefixes.length)];
        const suffix = Math.floor(Math.random() * 100000000).toString().padStart(8, '0');
        return `${prefix}${suffix}`;
    }

    return Array.from({ length: count }, (_, index) => {
        const id = index + 1;
        const isMale = Math.random() > 0.5;
        const surname = surnames[Math.floor(Math.random() * surnames.length)];
        const firstNameArray = isMale ? firstNamesMale : firstNamesFemale;
        const firstName = firstNameArray[Math.floor(Math.random() * firstNameArray.length)];
        const department = departments[Math.floor(Math.random() * departments.length)];
        const position = positions[Math.floor(Math.random() * positions.length)];
        const phone = generateRandomPhone();
        const email = `${surname.toLowerCase()}${firstName.toLowerCase()}${Math.floor(Math.random() * 100)}@example.com`;

        return {
            id,
            name: surname + firstName,
            age: Math.floor(Math.random() * 50) + 18,
            sex: isMale ? '男' : '女',
            address: `上海市${districts[Math.floor(Math.random() * districts.length)]}${streets[Math.floor(Math.random() * streets.length)]} ${Math.floor(Math.random() * 10000) + 1000} 弄`,
            position,
            department,
            hireDate: generateRandomDate(),
            phone,
            email
        };
    });
}

// 注册数据生成任务（指定 handlerKey 为 'generate-table-data'）
registerHandler('generate-table-data', (count: number) => {    
    return generateTestData(count); // 调用生成函数并返回结果
});